const request = indexedDB.open('gameDB', 1); //创建 Indexed DB 数据库

request.onupgradeneeded = function(event) { //创建对象存储区
	const db = event.target.result;
	const objectStore = db.createObjectStore('highScores', {
		keyPath: 'id',
		autoIncrement: true
	});
	objectStore.createIndex('time', 'time', {
		unique: false
	});
	objectStore.createIndex('grade', 'grade', {
		unique: false
	});
};

request.onerror = function(event) { //数据库访问错误的回调函数
	console.log('打开数据库时发生错误');
};

request.onsuccess = function(event) { //数据库访问成功的回调函数
	const db = event.target.result;

	form.addEventListener('submit', function(event) { //监听表单数据
		event.preventDefault();
		const note = {
			title: title.value,
			content: content.value
		};
		const transaction = db.transaction(['highScores'], 'readwrite');
		const objectStore = transaction.objectStore('highScores');
		const request = objectStore.add(note);

		request.onsuccess = function() {
			displayNotes();
			title.value = '';
			content.value = '';
		};

		transaction.oncomplete = function() {
			console.log('笔记已添加');
		};

		transaction.onerror = function(event) {
			console.log('添加笔记时发生错误');
		};
	});
}